package com.zareal.ymcc.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.zareal.ymcc.domain.Course;
import com.zareal.ymcc.domain.KillActivity;
import com.zareal.ymcc.domain.KillCourse;
import com.zareal.ymcc.dto.CourseDto;
import com.zareal.ymcc.enums.GlobalErrorCode;
import com.zareal.ymcc.feign.CourseServiceFeignClient;
import com.zareal.ymcc.mapper.KillCourseMapper;
import com.zareal.ymcc.result.JSONResult;
import com.zareal.ymcc.service.IKillActivityService;
import com.zareal.ymcc.service.IKillCourseService;
import com.zareal.ymcc.util.AssertUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Wilson
 * @since 2022-10-20
 */
@Service
@Slf4j
public class KillCourseServiceImpl extends ServiceImpl<KillCourseMapper, KillCourse> implements IKillCourseService {
    @Autowired
    private CourseServiceFeignClient courseService;

    @Autowired
    private IKillActivityService killActivityService;
    /**
     * 课程秒杀
     * @param killCourse
     */
    @Override
    public void seckill(KillCourse killCourse) {
        Date now = new Date();
        // 1.用jsr303判断不为空
        // 2.根据课程Id查询课程是否存在，是否上架
        JSONResult jsonResult = courseService.get(killCourse.getCourseId());
        AssertUtil.isTrue(jsonResult.isSuccess(), GlobalErrorCode.COURSE_IS_NULL);
        Object data = jsonResult.getData();
        AssertUtil.isNotNull(data, GlobalErrorCode.COURSE_IS_NULL);
        CourseDto courseDto = JSON.parseObject(JSON.toJSONString(data), CourseDto.class);
        Course course = courseDto.getCourse();
        // 2.1判断是否上架
        boolean boo = course.getStatus() == Course.COURSE_ONLINE;
        AssertUtil.isTrue(boo, GlobalErrorCode.COURSE_ALREADY_OFFLINE);
        // 3.1判断活动是否上架
        KillActivity killActivity = killActivityService.selectById(killCourse.getActivityId());
        AssertUtil.isNotNull(killActivity,GlobalErrorCode.ACTIVITY_ALREADY_OFFLINE);

        // 3.2 判断秒杀活动是不是等待发布
        boolean killBoo = killActivity.getPublishStatus() == KillActivity.WAIT_PUBLISH;
        AssertUtil.isTrue(killBoo, GlobalErrorCode.ACTIVITY_ALREADY_ONLINE);

        // 3.3判断活动中是否已经有这个课程了
        KillCourse killCourseTmp = CourseselectKillCourseByKillActivityIdAndCourseId(killActivity.getId(),killCourse.getCourseId());
        AssertUtil.isNull(killCourseTmp,GlobalErrorCode.KILLCOURSE_ALREADY_ONLINE);
        // 4.设置课程到秒杀课程中
        initKillCourse(killCourse,killActivity);
    }
    //todo
    @Override
    public List<KillCourse> online() {
        return null;
    }



    private void initKillCourse(KillCourse killCourse, KillActivity killActivity) {
        killCourse.setCreateTime(new Date());
        killCourse.setStartTime(killActivity.getBeginTime());
        killCourse.setEndTime(killActivity.getEndTime());
        killCourse.setKillLimit(1);
        killCourse.setTimeStr(killActivity.getTimeStr());
        killCourse.setPublishStatus(killActivity.getPublishStatus());
    }

    private KillCourse CourseselectKillCourseByKillActivityIdAndCourseId(Long killActivityId, Long killCourseId) {
        Wrapper<KillCourse> wrapper = new EntityWrapper<>();
        wrapper.eq("activity_id",killActivityId);
        wrapper.eq("course_id",killCourseId);
        return selectOne(wrapper);
    }
}
